-
Notifications
You must be signed in to change notification settings - Fork 1.1k
DATAMONGO-1849 - Resolve JsonSchema from domain type. #733
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
JsonSchemaCreator extracts the MongoJsonSchema for a given Class by applying the following mapping rules: Required Properties: - All Constructor arguments annotated with Nullable. - Properties of primitive type. Ignored Properties: - All properties annotated with Transient. Property Type Mapping: - java.lang.Object -> { type : 'object' } - java.util.Arrays -> { type : 'array' } - java.util.Collection -> { type : 'array'} - java.util.Map -> { type : 'object'} - java.lang.Enum -> { type : 'string', enum : [ ... ] } - Simple Types -> { type : 'the corresponding bson type' } - Domain Types -> { type : 'object', properties : { ... } } _id properties using types that can be converted into ObjectId like String will be mapped to { type : 'object' } unless there is more specific information available via the MongoId annotation.
47bb7a7
to
cd9d764
Compare
By using Field#targetType it is now possible to pass down a type hint to the conversion subsystem. This allows specifying the desired target type for a property so that eg. a plain String can be stored as Code.
0468059
to
c9097c9
Compare
|
||
private boolean isRequiredProperty(PersistentEntity<?, ?> parent, PersistentProperty property) { | ||
|
||
return (parent.isConstructorArgument(property) && !property.isAnnotationPresent(Nullable.class)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See NullableUtils
: Properties are nullable by default. Annotating a package with @NonNullApi
or @NonNullFields
switches to non-null by default.
* @param mongoConverter must not be {@literal null}. | ||
* @return new instance of {@link MongoJsonSchemaCreator}. | ||
*/ | ||
default MongoJsonSchemaCreator jsonSchemaCreator(MongoConverter mongoConverter) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be static
?
@@ -1667,4 +1689,91 @@ private static boolean isCollectionOfDbRefWhereBulkFetchIsPossible(Iterable<?> s | |||
return null; | |||
} | |||
} | |||
|
|||
private static class HijackedTypeInformation<S> implements TypeInformation<S> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about TypeInformationWrapper
😬and handling the rest of customizations in a separated, smaller type?
* @return never {@literal null}. | ||
* @since 2.2 | ||
*/ | ||
default Class<?> computeWriteTarget(Class<?> source) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should improve either on the definition or on the implementation of this method. Right now, we could assume that the write target is either a native Mongo type or the write target is an to be converted type.
The number of exposed methods via MongoWriter
and MongoConverter
keeps constantly increasing and I'm not sure how to think about it.
Fix generics usage in MappingMongoJsonSchemaCreator. Make fields final. Rename MappingMongoConverter.computeWriteTarget to getWriteTarget and expose it publicly for reuse in custom DefaultTypeMapper setups without the need to subclass MappingMongoConverter. Remove Nullability functionality for required fields as nullability indicators should originate from PersistentProperty and PreferredConstructor. Related ticket: DATACMNS-1513
MongoJsonSchemaCreator extracts the MongoJsonSchema for a given Class by applying the following mapping rules: Required Properties: - Properties of primitive type. Ignored Properties: - All properties annotated with Transient. Property Type Mapping: - java.lang.Object -> { type : 'object' } - java.util.Arrays -> { type : 'array' } - java.util.Collection -> { type : 'array'} - java.util.Map -> { type : 'object'} - java.lang.Enum -> { type : 'string', enum : [ ... ] } - Simple Types -> { type : 'the corresponding bson type' } - Domain Types -> { type : 'object', properties : { ... } } _id properties using types that can be converted into ObjectId like String will be mapped to { type : 'object' } unless there is more specific information available via the MongoId annotation. By using Field#targetType it is now possible to pass down a type hint to the conversion subsystem. This allows specifying the desired target type for a property so that eg. a plain String can be stored as Code. Original pull request: #733.
Fix generics usage in MappingMongoJsonSchemaCreator. Make fields final. Rename MappingMongoConverter.computeWriteTarget to getWriteTarget and expose it publicly for reuse in custom DefaultTypeMapper setups without the need to subclass MappingMongoConverter. Remove Nullability functionality for required fields as nullability indicators should originate from PersistentProperty and PreferredConstructor. Update documentation. Related ticket: DATACMNS-1513 Original pull request: #733.
That's merged and polished now. |
@@ -75,7 +76,9 @@ static Collection<Object> getConvertersToRegister() { | |||
List<Object> converters = new ArrayList<>(); | |||
|
|||
converters.add(BigDecimalToStringConverter.INSTANCE); | |||
converters.add(BigDecimalToDecimal128Converter.INSTANCE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has no effect, as BigDecimalToStringConverter.INSTANCE
from above always takes precedence. All BigDecimal end up as String in the database.
Why do we still need BigDecimalToStringConverter
?
JsonSchemaCreator
extracts theMongoJsonSchema
for a givenClass
by applying the following mapping rules:Required Properties
Ignored Properties
Property Type Mapping:
{ type : 'object' }
{ type : 'array' }
{ type : 'array'}
{ type : 'object'}
{ type : 'string', enum : [ ... ] }
{ type : 'the corresponding bson type' }
{ type : 'object', properties : { ... } }
_id
properties using types that can be converted intoObjectId
likeString
will be mapped to{ type : 'object' }
unless there is more specific information available via theMongoId
annotation.